//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "EquityFXVolSurface.h"
using namespace Cephei::QL::Experimental::Volatility;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Experimental/Volatility/BlackVolSurface.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures::Volatility;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::CEquityFXVolSurface (boost::shared_ptr<QuantLib::EquityFXVolSurface>& childNative, Object^ owner) : CBlackVolSurface(CEquityFXVolSurface::typeid)
{
#ifdef HANDLE
	_phEquityFXVolSurface = NULL;
#endif
	_ppEquityFXVolSurface = &childNative;
    _ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (boost::dynamic_pointer_cast<QuantLib::BlackVolSurface> (*_ppEquityFXVolSurface));
}
Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::CEquityFXVolSurface (QuantLib::EquityFXVolSurface& childNative, Object^ owner) : CBlackVolSurface(CEquityFXVolSurface::typeid)
{
#ifdef HANDLE
	_phEquityFXVolSurface = NULL;
#endif
	_ppEquityFXVolSurface = new boost::shared_ptr<QuantLib::EquityFXVolSurface> (&childNative);
    _ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (boost::dynamic_pointer_cast<QuantLib::BlackVolSurface> (*_ppEquityFXVolSurface));
    _EquityFXVolSurfaceOwner = owner;
    _BlackVolSurfaceOwner = owner;
}

Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::CEquityFXVolSurface (CEquityFXVolSurface^ copy) : CBlackVolSurface(CEquityFXVolSurface::typeid)
{
#ifdef HANDLE
	_phEquityFXVolSurface = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEquityFXVolSurface = new boost::shared_ptr<QuantLib::EquityFXVolSurface> (copy->GetShared());
        _ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (boost::dynamic_pointer_cast<QuantLib::BlackVolSurface> (*_ppEquityFXVolSurface));
    }
}
Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::CEquityFXVolSurface (System::Type^ t) : CBlackVolSurface(CEquityFXVolSurface::typeid)
{
#ifdef HANDLE
	_phEquityFXVolSurface = NULL;
#endif
	if (!t->IsSubclassOf(CEquityFXVolSurface::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::CEquityFXVolSurface (QuantLib::Handle<QuantLib::EquityFXVolSurface>& childNative, Object^ owner)  : CBlackVolSurface(CEquityFXVolSurface::typeid)
{
	_phEquityFXVolSurface = &childNative;
	_ppEquityFXVolSurface = &static_cast<boost::shared_ptr<QuantLib::EquityFXVolSurface>>(childNative.currentLink());
    _ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (boost::dynamic_pointer_cast<QuantLib::BlackVolSurface> (*_ppEquityFXVolSurface));
    _EquityFXVolSurfaceOwner = owner;
}
Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::CEquityFXVolSurface (QuantLib::Handle<QuantLib::EquityFXVolSurface> childNative)  : CBlackVolSurface(CEquityFXVolSurface::typeid)
{
	_phEquityFXVolSurface = &childNative;
	_ppEquityFXVolSurface = &static_cast<boost::shared_ptr<QuantLib::EquityFXVolSurface>>(childNative.currentLink());
    _ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (boost::dynamic_pointer_cast<QuantLib::BlackVolSurface> (*_ppEquityFXVolSurface));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::CEquityFXVolSurface (QuantLib::EquityFXVolSurface childNative)  : CBlackVolSurface(CEquityFXVolSurface::typeid)
{
#ifdef HANDLE
	_phEquityFXVolSurface = NULL;
#endif
	_ppEquityFXVolSurface = new boost::shared_ptr<QuantLib::EquityFXVolSurface> (new QuantLib::EquityFXVolSurface (childNative));
    _ppBlackVolSurface = new boost::shared_ptr<QuantLib::BlackVolSurface> (boost::dynamic_pointer_cast<QuantLib::BlackVolSurface> (*_ppEquityFXVolSurface));
}
#endif

Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::~CEquityFXVolSurface ()
{
    if (_ppEquityFXVolSurface != NULL)
    {
	    delete _ppEquityFXVolSurface;
        _ppEquityFXVolSurface = NULL;
    }
}
Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::!CEquityFXVolSurface ()
{
    if (_ppEquityFXVolSurface != NULL)
    {
	    delete _ppEquityFXVolSurface;
    }
}
QuantLib::EquityFXVolSurface& Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::GetReference ()
{
    if (_ppEquityFXVolSurface == NULL) throw gcnew NativeNullException ();
	return **_ppEquityFXVolSurface;
}
boost::shared_ptr<QuantLib::EquityFXVolSurface>& Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::GetShared ()
{
    if (_ppEquityFXVolSurface == NULL) throw gcnew NativeNullException ();
	return *_ppEquityFXVolSurface;
}
QuantLib::EquityFXVolSurface* Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::GetPointer ()
{
    if (_ppEquityFXVolSurface == NULL) throw gcnew NativeNullException ();
	return &**_ppEquityFXVolSurface;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::EquityFXVolSurface>& Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::GetHandle ()
{
	if (_phEquityFXVolSurface == NULL)
	{
		_phEquityFXVolSurface = new Handle<QuantLib::EquityFXVolSurface> (*_ppEquityFXVolSurface);
	}
	return *_phEquityFXVolSurface;
}
#endif
bool Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::HasNative () 
{
	return (_ppEquityFXVolSurface != NULL);
}

Double Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::AtmForwardVariance (Double time1, Double time2, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _time1 = (QuantLib::Time)ValueHelper::Convert (time1);
        QuantLib::Time _time2 = (QuantLib::Time)ValueHelper::Convert (time2);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppEquityFXVolSurface)->atmForwardVariance ( _time1,  _time2,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::AtmForwardVariance (DateTime date1, DateTime date2, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Date _date1 = (QuantLib::Date)ValueHelper::Convert (date1);
        QuantLib::Date _date2 = (QuantLib::Date)ValueHelper::Convert (date2);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppEquityFXVolSurface)->atmForwardVariance ( _date1,  _date2,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::AtmForwardVol (Double time1, Double time2, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _time1 = (QuantLib::Time)ValueHelper::Convert (time1);
        QuantLib::Time _time2 = (QuantLib::Time)ValueHelper::Convert (time2);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppEquityFXVolSurface)->atmForwardVol ( _time1,  _time2,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Volatility::CEquityFXVolSurface::AtmForwardVol (DateTime date1, DateTime date2, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Date _date1 = (QuantLib::Date)ValueHelper::Convert (date1);
        QuantLib::Date _date2 = (QuantLib::Date)ValueHelper::Convert (date2);
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppEquityFXVolSurface)->atmForwardVol ( _date1,  _date2,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

